package com.alibaba.nacos.config.server;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class BusinessOpt {

    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    private final Map<String, List<PathPlanServerResponseInfo>> clientIdServerResponseMap = new ConcurrentHashMap<>(128);



    public void addResponse(final String id, PathPlanServerResponseInfo pathPlanServerResponseInfo) {
        if (null == pathPlanServerResponseInfo) {
            return;
        }
        try {
            lock.writeLock().lockInterruptibly();
            List<PathPlanServerResponseInfo> pathPlanServerResponseInfos = clientIdServerResponseMap.get(id);
            if (pathPlanServerResponseInfos != null && !pathPlanServerResponseInfos.isEmpty()) {
                pathPlanServerResponseInfos.add(pathPlanServerResponseInfo);
            } else {
                pathPlanServerResponseInfos = new ArrayList<>();
                pathPlanServerResponseInfos.add(pathPlanServerResponseInfo);
                clientIdServerResponseMap.put(id, pathPlanServerResponseInfos);
            }
        } catch (Exception e) {
            System.err.println("[车联网 - 路径规划] 暂存返回信息异常" + e);
            e.printStackTrace();
        } finally {
            System.out.println(Thread.currentThread().getName() + "  ----  unlock");
            lock.writeLock().unlock();
        }
    }

    static class PathPlanServerResponseInfo implements Serializable {

        private static final long serialVersionUID = -3428413927226480374L;

        private String address;

        private String version;

        private String type;

        private Object data;

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public Object getData() {
            return data;
        }

        public void setData(Object data) {
            this.data = data;
        }
    }

}
